<!DOCTYPE html>
<html lang='en'>

<head>
    <meta charset='UTF-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Document</title>
    <script src='../vue.js'></script>
</head>

<body>
    <div id='app'>
        <!-- v-model="search数据双向绑定 -->
        <input type="text" v-model="search"/>
        <!-- 列表渲染 -->
        <ul>
            <li v-for="item in newPerson" :key="item.id">
                {{item.name}}---{{item.age}}
            </li>
        </ul>
    </div>
    <script>
        Vue.config.productionTip = false;
        const vm = new Vue({
            el: '#app',
            data() {
                return {
                    search:"",
                    pershon:[
                     {id:"001",name:"杨幂",age:33},
                     {id:"002",name:"迪丽热巴",age:27},
                     {id:"003",name:"刘亦菲",age:30},
                     {id:"004",name:"胡歌",age:32},
                     {id:"005",name:"杨洋",age:28},
                    ],
                    // 设置一个prerson为空的数组
                    newPerson:[],
                };
            },
            // watch是监听  immediate：立即  handler是处理的意思
                watch:{
                    search:{
                      immediate:true,
                      handler(newValue){
                        // filter：过滤的意思   又要监听，又要得到一个新的值
                        this.newPerson =this.pershon.filter((item)=>{
                            return item.name.includes(newValue)
                        });
                      },   
                    },
                },
          
        })
    </script>
</body>

</html> 